using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace homecontentmanagementapp.Features.Feature1
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("1d74c0d5-63ef-4a1e-a665-fa992e43cabf")]
    public class Feature1EventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            SPWeb web = site.RootWeb;
            string doclibName = properties.Feature.Properties["CreditCardStatements"].Value;
            DeleteDocumentContentType(web, doclibName);
            SetViewWithContentTypeFields(web, doclibName);

            doclibName = properties.Feature.Properties["FinancialStatements"].Value;
            DeleteDocumentContentType(web, doclibName);
            SetViewWithContentTypeFields(web, doclibName);

            doclibName = properties.Feature.Properties["Invoices"].Value;
            DeleteDocumentContentType(web, doclibName);
            SetViewWithContentTypeFields(web, doclibName);

            doclibName = properties.Feature.Properties["LegalDocuments"].Value;
            DeleteDocumentContentType(web, doclibName);
            SetViewWithContentTypeFields(web, doclibName);

            doclibName = properties.Feature.Properties["LoanDocuments"].Value;
            DeleteDocumentContentType(web, doclibName);
            SetViewWithContentTypeFields(web, doclibName);

            doclibName = properties.Feature.Properties["InsuranceDocuments"].Value;
            DeleteDocumentContentType(web, doclibName);
            SetViewWithContentTypeFields(web, doclibName);

            doclibName = properties.Feature.Properties["Receipts"].Value;
            DeleteDocumentContentType(web, doclibName);
            SetViewWithContentTypeFields(web, doclibName);

            doclibName = properties.Feature.Properties["PersonalDocuments"].Value;
            DeleteDocumentContentType(web, doclibName);
            SetViewWithContentTypeFields(web, doclibName);
        }

        private void SetViewWithContentTypeFields(SPWeb web, string doclibName)
        {
            SPContentType doclibContentType = null;
            SPDocumentLibrary list = web.Lists[doclibName] as SPDocumentLibrary;
            foreach (SPContentType ct in list.ContentTypes)
            {
                doclibContentType = ct;
            }
            doclibContentType = list.ContentTypes[0];
            SPView doclibView = list.DefaultView;
            doclibView.ViewFields.DeleteAll();
            doclibView.ViewFields.Add("Name");
            foreach (SPFieldLink link in doclibContentType.FieldLinks)
            {
                SPField field = list.Fields[link.Id];
                switch (field.InternalName)
                {
                    case "ContentType":
                    case "SelectFilename":
                    case "FileLeafRef":
                    case "Created":
                    case "Modified":
                    case "Modified_x0020_By":
                    case "Created_x0020_By":
                        break;
                    default:
                        doclibView.ViewFields.Add(field);
                        break;
                }
            }
            doclibView.Scope = SPViewScope.Recursive;
            doclibView.Update();
        }

        private void DeleteDocumentContentType(SPWeb web, string doclibName)
        {
            SPDocumentLibrary docLib = web.Lists.TryGetList(doclibName) as SPDocumentLibrary;
            SPContentType documentContentType = docLib.ContentTypes["Document"];
            if (documentContentType != null)
                documentContentType.Delete();
        }

        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
